using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text.RegularExpressions;

namespace Kiiro.Import
{
    internal enum WizardPage
    {
        Welcome,
        BasecampSettings,
        KiiroSettings,
        UserOptions,
        ProjectOptions,
        Projects,
        Review,
        ImportStatus,
        ImportComplete

    }

    internal enum WizardDirection
    {
        Next,
        Back,
        Cancel
    }


    public partial class frmImport : Form
    {

        KiiroImportSettings importSettings = new KiiroImportSettings();
        KiiroBasecampImport import = new Kiiro.Import.KiiroBasecampImport();
        WizardPage currentPage = new WizardPage();        
        string settingsFile = Application.StartupPath + "\\ImportSettings.txt";
        DataRow[] basecampProjectList = null;
        IList<KiiroUser> kiiroActiveUsers = null;


        public frmImport()
        {
            InitializeComponent();

            currentPage = WizardPage.Welcome;

            LoadImportSettings();

            importSettings.LocalCacheFolder = Application.StartupPath;
            importSettings.ImportLog = "";
            importSettings.SecurityWebServiceURL = "_layouts/kiiroproject.asmx";
            importSettings.DiscussionWebServiceURL = "_layouts/kiirodiscussion.asmx";
            importSettings.DocumentWebServiceURL = "_layouts/kiirodocument.asmx";
            importSettings.TaskWebServiceURL = "_layouts/kiirotask.asmx";
            importSettings.SecurityWebServiceURL = "_layouts/kiirosecurity.asmx";

            this.Text += " " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
            
            DisplayCurrentPage();

            import.ImportStatusChange += new KiiroBasecampImport.ImportStatusHandler(ImportStatusChange);
           }

        

        private void ImportStatusChange(ImportStage stage, int current, int total, int percentComplete, string description)
        {
            //update the labels and progress bars
            switch (stage)
            {
                case ImportStage.ImportingProject:
                    lblProjectStatus.Text = "Importing Project " + current.ToString() + " of " + total.ToString() + ":";

                    if (description.Length > 60)
                        lblImportingProjectName.Text = description.Substring(0,60) + "...";
                    else
                        lblImportingProjectName.Text = description;
                    prgStatus.Value = 0;
                    lblStatus.Visible = false;
                    lblStatusCount.Visible = false;

                    break;
                case ImportStage.DownloadingFiles:
                    prgStatus.Visible = true;
                    lblStatusCount.Visible = true;
                    lblStatus.Visible = true;

                    prgStatus.Maximum = total;
                    prgStatus.Value = current;
                    
                    if (total == 0)
                        lblStatusCount.Text = "";
                    else
                        lblStatusCount.Text = current.ToString() + " of " + total.ToString();
                    lblStatus.Text = "Exporting Files:";
                    break;
                    
                case ImportStage.DownloadingMessages:
                    prgStatus.Visible = true;
                    lblStatusCount.Visible = true;
                    lblStatus.Visible = true;

                    prgStatus.Maximum = total;
                    prgStatus.Value = current;
                    
                    if (total == 0)
                        lblStatusCount.Text = "";
                    else
                        lblStatusCount.Text = current.ToString() + " of " + total.ToString();
                    lblStatus.Text = "Exporting Messages:";
                    break;
                    

                case ImportStage.ImportingMessages:
                    prgStatus.Visible = true;
                    lblStatusCount.Visible = true;
                    lblStatus.Visible = true;

                    prgStatus.Maximum = total;
                    prgStatus.Value = current;
                    
                    if (total == 0)
                        lblStatusCount.Text = "";
                    else
                        lblStatusCount.Text = current.ToString() + " of " + total.ToString();
                    lblStatus.Text = "Importing Messages:";
                    break;

                case ImportStage.DownloadingMilestones:
                    prgStatus.Visible = true;
                    lblStatusCount.Visible = true;
                    lblStatus.Visible = true;

                    prgStatus.Maximum = total;
                    prgStatus.Value = current;

                    if (total == 0)
                        lblStatusCount.Text = "";
                    else
                        lblStatusCount.Text = current.ToString() + " of " + total.ToString();
                    lblStatus.Text = "Exporting Milestones:";
                    break;

                case ImportStage.ImportingMilestones:
                    prgStatus.Visible = true;
                    lblStatusCount.Visible = true;
                    lblStatus.Visible = true;

                    prgStatus.Maximum = total;
                    prgStatus.Value = current;

                    if (total == 0)
                        lblStatusCount.Text = "";
                    else
                        lblStatusCount.Text = current.ToString() + " of " + total.ToString();
                    lblStatus.Text = "Importing Milestones:"; 
                    break;
                case ImportStage.ImportingFiles:
                    prgStatus.Visible = true;
                    lblStatusCount.Visible = true;
                    lblStatus.Visible = true;

                    prgStatus.Maximum = total;
                    prgStatus.Value = current;

                    if (total == 0)
                        lblStatusCount.Text = "";
                    else
                        lblStatusCount.Text = current.ToString() + " of " + total.ToString();
                    lblStatus.Text = "Importing Files:";                    
                    break;                                      

            }
            Application.DoEvents();
        }

        private void DisplayProgressBarCaption(ProgressBar bar, string caption)
        {
            bar.CreateGraphics().DrawString(caption, new Font("Arial", (float)8.25, FontStyle.Regular), Brushes.Black, new PointF(bar.Width / 2 - 10, bar.Height / 2 - 7));
        }

        public void SaveImportSettings()
        {
            Stream stream = File.Open(settingsFile, FileMode.Create);
            BinaryFormatter bFormatter = new BinaryFormatter();
            bFormatter.Serialize(stream, importSettings);
            stream.Close();
        }

        public void LoadImportSettings()
        {

            if (File.Exists(settingsFile))
            {
                Stream stream = File.Open(settingsFile, FileMode.Open);
                BinaryFormatter bFormatter = new BinaryFormatter();
                importSettings = (KiiroImportSettings)bFormatter.Deserialize(stream);
                stream.Close();
            }
            else
            {
                importSettings = new KiiroImportSettings();
            }
        }


        private void FileDownloadStatusChange(int current, int total, string fileName, int percentComplete)
        {
            prgStatus.Maximum = 100;
            prgStatus.Value = percentComplete;
            Application.DoEvents();
        }

        private void frmImport_Load(object sender, EventArgs e)
        {

        }

        private void butNext_Click(object sender, EventArgs e)
        {
            HandleWizardMove(WizardDirection.Next);
        }


        private void MoveBack()
        {

            switch (currentPage)
            {
                case WizardPage.BasecampSettings:
                    currentPage = WizardPage.Welcome;
                    break;
                case WizardPage.KiiroSettings:
                    currentPage = WizardPage.BasecampSettings;
                    break;
                case WizardPage.UserOptions:
                    currentPage = WizardPage.KiiroSettings;
                    break;
                case WizardPage.ProjectOptions:
                    currentPage = WizardPage.KiiroSettings;
                    break;
                case WizardPage.Projects:
                    currentPage = WizardPage.ProjectOptions;
                    break;
                case WizardPage.Review:
                    currentPage = WizardPage.Projects;
                    break;
                case WizardPage.ImportStatus:
                    currentPage = WizardPage.Review;
                    break;
            }

            DisplayCurrentPage();

        }

        private void DisplayProjectOptions()
        {         
               
            this.Cursor = Cursors.WaitCursor;

            if (kiiroActiveUsers == null)
                kiiroActiveUsers = import.GetKiiroUserList(importSettings);

            cboDefaultUser.Items.Clear();
            foreach (KiiroUser user in kiiroActiveUsers)
            {

                if (user.CanCreateProjects)
                {
                    cboDefaultUser.Items.Add(user);

                    //compare to default already chosen
                    if (importSettings.DefaultKiiroUser != null)
                        if (importSettings.DefaultKiiroUser.ID == user.ID)
                            cboDefaultUser.SelectedItem = user;
                }
            }

            chkImportMilestones.Checked = importSettings.ImportMilestones;
            chkImportMessages.Checked = importSettings.ImportMessages;
            chkImporFiles.Checked = importSettings.ImportFiles;
            chkDeleteExistingProjects.Checked = importSettings.DeleteExistingProjects;
            chkReuseFiles.Checked = importSettings.ReuseFiles;

            this.Cursor = Cursors.Default;
            
        }

        private void DisplayProjects()
        {
            string[] items = null;
            ListViewItem item = null;
            DataTable dt = new DataTable();
            DataRow[] sorted = null;

            lstProjects.Items.Clear();


            this.Cursor = Cursors.WaitCursor;

            //get and sort the project data
            if (basecampProjectList == null)
            {
                dt = GetBasecampProjectDatatable(importSettings);
                sorted = dt.Select("", "Company ASC, Project ASC");
                basecampProjectList = sorted;
            }

            //display the data
            foreach (DataRow dr in basecampProjectList)
            {
                items = new string[] {(string)dr["Company"], (string)dr["Project"], ((DateTime)dr["LastUpdated"]).ToShortDateString(), ((DateTime)dr["Created"]).ToShortDateString(), (string)dr["Status"] };
                item = new ListViewItem();
                item.UseItemStyleForSubItems = false;
                item.SubItems.AddRange(items);                
                item.SubItems[2].ForeColor = Color.Blue;
                item.Text = "";

                //determine if the item has been selected before
                if (importSettings.ProjectsToImport != null)
                {
                    foreach (BasecampProject proj in importSettings.ProjectsToImport)
                    {
                        if (Int32.Parse(proj.ID) == (Int32)dr["ID"])
                        {
                            item.Checked = true;
                            break;
                        }
                    }
                }
                

                lstProjects.Items.Add(item);

                
            }

            this.Cursor = Cursors.Default;

        }

        private DataTable GetBasecampProjectDatatable(KiiroImportSettings importSettings)
        {
            IList<BasecampProject> projects;
            DataTable dt = new DataTable();
            object[] row = new object[7];

            dt.Columns.Add("Project", Type.GetType("System.String"));
            dt.Columns.Add("Company", Type.GetType("System.String"));
            dt.Columns.Add("Status", Type.GetType("System.String"));
            dt.Columns.Add("Created", Type.GetType("System.DateTime"));
            dt.Columns.Add("LastUpdated", Type.GetType("System.DateTime"));
            dt.Columns.Add("ID", Type.GetType("System.Int32"));
            dt.Columns.Add("Data", Type.GetType("System.Object"));
            
            
            projects = import.GetBasecampProjectList(importSettings);

            foreach (BasecampProject project in projects)
            {                
                row[0] = project.ProjectName;
                row[1] = project.CompanyName;
                project.Status = project.Status.Substring(0,1).ToUpper() + project.Status.Substring(1, project.Status.Length - 1);
                row[2] = project.Status;                
                row[3] = project.CreatedDate;
                row[4] = project.LastUpdatedDate;
                row[5] = project.ID;
                row[6] = project;
                dt.Rows.Add(row);
            }

            return dt;

        }


        private void HandleWizardMove(WizardDirection direction)
        {
            bool successfulSave = false;

            if (direction == WizardDirection.Next)
            {
                //Based on the current page, save and validate the settings
                switch (currentPage)
                {
                    case WizardPage.Welcome:
                        successfulSave = true;
                        break;
                    case WizardPage.BasecampSettings:
                        successfulSave = SaveBasecampSettings();
                        break;
                    case WizardPage.KiiroSettings:
                        successfulSave = SaveKiiroSettings();
                        break;
                    case WizardPage.UserOptions:
                        successfulSave = true;
                        break;
                    case WizardPage.ProjectOptions:
                        successfulSave = SaveProjectOptions();
                        break;
                    case WizardPage.Projects:
                        successfulSave = SaveProjects();
                        break;
                    case WizardPage.Review:
                        successfulSave = true;
                        break;
                    case WizardPage.ImportStatus:
                        successfulSave = true;
                        break;
                }

                if (successfulSave)
                {
                    SaveImportSettings();
                    MoveNext();
                }
            }
            else
            {
                MoveBack();
            }

        }



        private void MoveNext()
        {

            switch (currentPage)
            {
                case WizardPage.Welcome:
                    currentPage = WizardPage.BasecampSettings;
                    break;
                case WizardPage.BasecampSettings:
                    currentPage = WizardPage.KiiroSettings;
                    break;
                case WizardPage.KiiroSettings:
                    currentPage = WizardPage.ProjectOptions;
                    break;
                case WizardPage.UserOptions:
                    currentPage = WizardPage.ProjectOptions;
                    break;
                case WizardPage.ProjectOptions:
                    currentPage = WizardPage.Projects;
                    break;
                case WizardPage.Projects:
                    currentPage = WizardPage.Review;
                    break;                
                case WizardPage.Review:
                    currentPage = WizardPage.ImportStatus;
                    break;
                case WizardPage.ImportStatus:
                    currentPage = WizardPage.ImportComplete;
                    break;                
            }

            DisplayCurrentPage();

        }

        private void DoImport()
        {

            this.Cursor = Cursors.WaitCursor;

            import.ImportToKiiro(importSettings);

            this.Cursor = Cursors.Default;

            HandleWizardMove(WizardDirection.Next);

        }

        private void DisplayCurrentPage()
        {
            switch (currentPage)
            {
                case WizardPage.Welcome:
                    pnl1_Welcome.BringToFront();
                    break;
                case WizardPage.BasecampSettings:
                    DisplayBasecampSettings();
                    pnl2_BasecampSettings.BringToFront();
                    break;
                case WizardPage.KiiroSettings:
                    DisplayKiiroSettings();
                    pnl3_KiiroSettings.BringToFront();
                    break;
                case WizardPage.UserOptions:
                    pnl4_UserOptions.BringToFront();
                    break;
                case WizardPage.ProjectOptions:
                    DisplayProjectOptions();
                    pnl5_ProjectOptions.BringToFront();                    
                    break;
                case WizardPage.Projects:
                    DisplayProjects();
                    pnl6_Projects.BringToFront();                    
                    break;
                case WizardPage.Review:
                    DisplayReviewSummary();
                    pnl7_Review.BringToFront();
                    break;
                case WizardPage.ImportStatus:
                    DisplayImportStatus();
                    pnl8_Status.BringToFront();
                    Application.DoEvents();
                    butNext.Enabled = false;
                    butBack.Enabled = false;
                    DoImport();                    
                    break;
                case WizardPage.ImportComplete:
                    DisplayImportComplete();
                    pnl9_ImportComplete.BringToFront();
                    Application.DoEvents();
                    break;

            }

            if (currentPage == WizardPage.Welcome)
                butBack.Enabled = false;           
            else if (currentPage != WizardPage.ImportComplete)
            {
                butBack.Enabled = true;
                butNext.Enabled = true;
            }

            Application.DoEvents();
        }

        private void butBack_Click(object sender, EventArgs e)
        {
            HandleWizardMove(WizardDirection.Back);
        }

        private void DisplayBasecampSettings()
        {
            txtBasecampURL.Text = importSettings.BasecampURL;
            txtLogin.Text = importSettings.BasecampLogin;
            txtPassword.Text = importSettings.BasecampPassword;
        }

        private void DisplayKiiroSettings()
        {
            txtKiiroURL.Text = importSettings.KiiroURL;
        }

        private void DisplayReviewSummary()
        {
            txtImportSummary.Text = importSettings.ToString();
        }

        private void DisplayImportStatus()
        {
            prgStatus.Visible = false;
            lblStatus.Visible = false;
            lblStatusCount.Visible = false;
        }

        
        private void DisplayImportComplete()
        {

            txtImportLog.Text = importSettings.ImportLog;
            butNext.Enabled = false;
            butBack.Enabled = false;
            butCancel.Text = "Finish";

            if (importSettings.Success)
            {
                picImportSuccess.Visible = true;
                picImportSuccess.BringToFront();
                lblImportComplete.Text = "Import Completed Successfully!";
                txtImportLog.Visible = false;
                lblViewLog.Visible = true;
            }
            else
            {
                picImportError.Visible = true;
                picImportError.BringToFront();
                lblImportComplete.Text = "Import Completed With Errors!";
                txtImportLog.Visible = true;
                lblViewLog.Visible = false;
            }
        }

        /// <summary>
        /// Gets the basecamp settings from the UI.
        /// </summary>
        private bool SaveBasecampSettings()
        {
            string missingFields = "";
            bool success = false;

            if (txtBasecampURL.Text.Trim() == "")
                missingFields = "Basecamp URL\n";
            
            else if (!IsURLValid(txtBasecampURL.Text.Trim()))
            {
                DisplayError("Please enter a valid Basecamp URL");
                txtBasecampURL.Focus();
            }

            if (txtLogin.Text.Trim() == "")
                missingFields += "Admin Login\n";

            if (txtPassword.Text.Trim() == "")
                missingFields += "Admin Password\n";

            if (missingFields != "")
                DisplayMissingFieldError (missingFields);
            else
            {
                importSettings.BasecampURL = txtBasecampURL.Text.Trim();
                importSettings.BasecampURL = importSettings.BasecampURL.ToLower().Replace("http://", "").Replace("https://", "");
                if (importSettings.BasecampURL.Substring(importSettings.BasecampURL.Length-1, 1) != "/")
                    importSettings.BasecampURL += "/";
                importSettings.BasecampLogin = txtLogin.Text.Trim();
                importSettings.BasecampPassword = txtPassword.Text.Trim();                
                
                //test the connection with Basecamp. This will also set the secure mode flag (if basecamp is using SSL).              
                importSettings.BasecampSecureMode = true;
                this.Cursor = Cursors.WaitCursor;

                if (!import.TestBasecampConnection(importSettings))
                    DisplayError("Could not connect to Basecamp. Please ensure the URL \nand login information is correct.");
                else
                {
                    success = true;
                    basecampProjectList = null;

                    
                }
                this.Cursor = Cursors.Default;
            }
            return success;
        }

        /// <summary>
        /// Gets the basecamp settings from the UI.
        /// </summary>
        private bool SaveKiiroSettings()
        {
            string missingFields = "";
            bool success = false;

            if (txtKiiroURL.Text.Trim() == "")
                missingFields = "Kiiro URL\n";

            else if (!IsURLValid(txtKiiroURL.Text.Trim()))
            {
                DisplayError("Please enter a valid Kiiro URL");
                txtBasecampURL.Focus();
            }           

            if (missingFields != "")
                DisplayMissingFieldError(missingFields);
            else
            {
                importSettings.KiiroURL = txtKiiroURL.Text.Trim();
                if (importSettings.KiiroURL.Substring(importSettings.KiiroURL.Length - 1, 1) != "/")
                    importSettings.KiiroURL += "/";

                if (!importSettings.KiiroURL.ToLower().StartsWith("http://") && !importSettings.KiiroURL.ToLower().StartsWith("https://"))
                    importSettings.KiiroURL = "http://" + importSettings.KiiroURL;

                //test the connection with Kiiro.
                this.Cursor = Cursors.WaitCursor;

                if (!import.TestKiiroConnection(importSettings))
                    DisplayError("Could not connect to Kiiro. Please ensure the URL is correct.");
                else
                {
                    success = true;
                }
                this.Cursor = Cursors.Default;
            }
            return success;
        }

        /// <summary>
        /// Gets the basecamp settings from the UI.
        /// </summary>
        private bool SaveProjectOptions()
        {
            string missingFields = "";
            bool success = false;

            if (!chkImporFiles.Checked && !chkImportMessages.Checked && !chkImporFiles.Checked)
                DisplayInfo("Please select to import at least one kind of item from Basecamp.");
            else
            {
                if (cboDefaultUser.Text.Trim() == "")
                    missingFields = "Default Kiiro User\n";

                if (missingFields != "")
                    DisplayMissingFieldError(missingFields);
                else
                {
                    importSettings.DefaultKiiroUser = ((KiiroUser)cboDefaultUser.SelectedItem);
                    importSettings.ImportFiles = chkImporFiles.Checked;
                    importSettings.ImportMessages = chkImportMessages.Checked;
                    importSettings.ImportMilestones = chkImportMilestones.Checked;
                    importSettings.DeleteExistingProjects = chkDeleteExistingProjects.Checked;
                    importSettings.ReuseFiles = chkReuseFiles.Checked;
                    success = true;

                }
            }
            return success;
        }

        /// <summary>
        /// Gets the basecamp settings from the UI.
        /// </summary>
        private bool SaveProjects()
        {
            bool success = false;

            //build a collection of selected items
            if (lstProjects.CheckedItems.Count == 0)
                DisplayInfo("Please select at least one project to import.");
            else
            {
                importSettings.ProjectsToImport = new List<BasecampProject>();
                foreach (ListViewItem checkedItem in lstProjects.CheckedItems)
                {
                    importSettings.ProjectsToImport.Add((BasecampProject)basecampProjectList[checkedItem.Index]["Data"]);
                }
                success = true;

            }
            return success;
        }

        private bool IsURLValid(string url)
        {
            Regex regEx = new Regex("(([a-zA-Z][0-9a-zA-Z+\\-\\.]*:)?/{0,2}[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?(#[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?");
            return regEx.IsMatch(url);
        }

        private void DisplayError(string error)
        {
            this.Cursor = Cursors.Default;
            MessageBox.Show(error, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        private void DisplayInfo(string error)
        {
            this.Cursor = Cursors.Default;
            MessageBox.Show(error, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void DisplayMissingFieldError(string missingFields)
        {
            this.Cursor = Cursors.Default;
            MessageBox.Show("Please provide a value for the following before clicking Next:\n\n" + missingFields, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void txtLogin_TextChanged(object sender, EventArgs e)
        {

        }



        private void lstProjects_MouseClick(object sender, MouseEventArgs e)
        {
           
            
        }

        private void lstProjects_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void lstProjects_MouseUp(object sender, MouseEventArgs e)
        {
            ListViewHitTestInfo info = lstProjects.HitTest(e.Location);
            if (info.SubItem != null)
                if (info.SubItem.ForeColor == Color.Blue)
                    System.Diagnostics.Process.Start(importSettings.BasecampFullURL + "projects/" + basecampProjectList[info.Item.Index]["ID"].ToString());

        }

        private void label25_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://help.getkiiro.com");
        }

        private void lstProjects_MouseHover(object sender, EventArgs e)
        {

        }

        private void lstProjects_MouseMove(object sender, MouseEventArgs e)
        {
            ListViewHitTestInfo info = lstProjects.HitTest(e.Location);
            if (info.Item != null)
            {
                if (info.SubItem.ForeColor == Color.Blue)
                    this.Cursor = Cursors.Hand;
                else
                {
                    this.Cursor = Cursors.Default;
                }
            }
                


        }

        private void lstProjects_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Default;
        }

        private void pnl6_Projects_MouseMove(object sender, MouseEventArgs e)
        {
            this.Cursor = Cursors.Default;
        }

        private void pnl8_Status_Paint(object sender, PaintEventArgs e)
        {

        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {

        }

        private void lblImportCompleteTitle_Click(object sender, EventArgs e)
        {

        }

        private void pictureBox12_Click(object sender, EventArgs e)
        {

        }

        private void lblViewLog_Click(object sender, EventArgs e)
        {
            lblViewLog.Visible = false;
            txtImportLog.Visible = true;

        }

        private void butCancel_Click(object sender, EventArgs e)
        {
            if (currentPage == WizardPage.ImportComplete)
                this.Close();

            else if (MessageBox.Show("Are you show you wish to cancel?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                this.Close();
        }

        private void lblStatus_Click(object sender, EventArgs e)
        {

        }

        private void prgStatus_Click(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }


        
        //private void ImportStatusChange(string status)
        //{
        //    listProjects.Items.Add(status);
        //    listProjects.SelectedItems.Clear();
        //    listProjects.SelectedIndex = listProjects.Items.Count - 1;
        //    Application.DoEvents();
        //}

        //private void ImportLogEventHandler(string message)
        //{
        //    listProjects.Items.Add("-----" + message);
        //    listProjects.SelectedItems.Clear();
        //    listProjects.SelectedIndex = listProjects.Items.Count - 1;
        //    Application.DoEvents();
        //}

        //private void button1_Click(object sender, EventArgs e)
        //{

        //    Cursor.Current = Cursors.WaitCursor;
            
        //    IList<BasecampProject> basecampProjects = new List<BasecampProject>();
        //    BasecampConnectionParams connectionParams = new BasecampConnectionParams(basecampURL, basecampLogin, basecampPassword, basecampSecureMode);
        //    KiiroBasecampImport import = new KiiroBasecampImport();


        //    //get a list of the projects in basecamp
        //    basecampProjects = import.GetProjectList(connectionParams);

        //    //display the projects in the list
        //    listProjects.Items.Clear();
        //    foreach (BasecampProject project in basecampProjects)
        //    {
        //        listProjects.Items.Add(project.ProjectName + " (" + project.CompanyName + ")");
        //    }

        //    Cursor.Current = Cursors.Default;

        //}

        //private void button2_Click(object sender, EventArgs e)
        //{
        //    BasecampConnectionParams connectionParams = new BasecampConnectionParams(basecampURL, basecampLogin, basecampPassword, basecampSecureMode);           
        //    IList<BasecampFile> postAttachments = null;
        //    IList<BasecampFile> commentAttachments = null;
        //    IList<BasecampFile> projectFiles = null;
        //    IList<BasecampProject> projects = new List<BasecampProject>();

        //    projects.Add(new BasecampProject("2729741", "ABC", "DEF"));
        //    import.ImportToBasecamp(projects, Environment.CurrentDirectory, connectionParams);
            
        //}

        //private void button1_Click_1(object sender, EventArgs e)
        //{
        //    import.CreateProjectTest("http://localhost/kiiro", "Test", "Test");
        //}

        //private void button3_Click(object sender, EventArgs e)
        //{
        //    import.AddUserTest("http://localhost/kiiro", "edm.nform.ca", "genes");
        //}

        //private void frmImport_Load(object sender, EventArgs e)
        //{

        //}


    }
}